package com.zhengxiang.onlinexamination.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhengxiang.onlinexamination.dao.QuMapper;
import com.zhengxiang.onlinexamination.entity.Qu;
import com.zhengxiang.onlinexamination.entity.QuAnswer;
import com.zhengxiang.onlinexamination.entity.QuBank;
import com.zhengxiang.onlinexamination.entity.vo.QuVo;
import com.zhengxiang.onlinexamination.service.QuAnswerService;
import com.zhengxiang.onlinexamination.service.QuBankService;
import com.zhengxiang.onlinexamination.service.QuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * (ZQu)表服务实现类
 *
 * @author 
 * @since 2023-03-08 14:06:14
 */
@Service("zQuService")
@Transactional(rollbackFor = Exception.class)
public class QuServiceImpl extends ServiceImpl<QuMapper, Qu> implements QuService {
    @Resource
    private QuMapper quMapper;

    @Autowired
    private QuAnswerService quAnswerService;

    @Autowired
    private QuBankService quBankService;

    @Override
    public IPage queryAll(QuVo quVo) {
        QueryWrapper<Qu> q = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(quVo.getSearchValue())){
            q.lambda().like(Qu::getContent,quVo.getSearchValue());
        }
        if(Objects.nonNull(quVo.getQuBankId())){
            q.eq("qu_bank_id",quVo.getQuBankId());
        }
        if(Objects.nonNull(quVo.getStartDate()) && Objects.nonNull(quVo.getEndDate())){
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String startDate = dateFormat.format(quVo.getStartDate());
            String endDate = dateFormat.format(quVo.getEndDate());
                q.ge("DATE_FORMAT(create_time,'%Y-%m-%d')",startDate);
                q.le("DATE_FORMAT(create_time,'%Y-%m-%d')",endDate);
        }
        q.lambda().orderByDesc(Qu::getUpdateTime);
        Page<Qu> zQuPage = quMapper.queryAll(new Page<Qu>(quVo.getCurrent(), quVo.getSize()), q);
        return zQuPage;
    }


    @Override
    public void saveQu(QuVo quVo) {
        quMapper.insert(quVo);
        if(CollectionUtil.isNotEmpty(quVo.getQuAnswers())){
            quVo.getQuAnswers().forEach(s->{
                s.setQuId(quVo.getQuId());
            });
            quAnswerService.saveBatch(quVo.getQuAnswers());
        }
        //更新题库题目数量记录
        QuBank qb = quBankService.getById(quVo.getQuBankId());
        switch (quVo.getQuType()){
            case 1:
                qb.setSingleQu(qb.getSingleQu()+1);
                break;
            case 2:
                qb.setMultipleQu(qb.getMultipleQu()+1);
                break;
            case 3:
                qb.setJudgmentQu(qb.getJudgmentQu()+1);
                break;
            case 4:
                qb.setShortQu(qb.getShortQu()+1);
                break;
            default:
                break;
        }
        quBankService.updateById(qb);
    }

    @Override
    public QuVo queryInfo(Integer quId) {
        return quMapper.queryInfo(quId);
    }

    @Override
    public void updateQu(QuVo quVo) {
        quMapper.updateById(quVo);
        UpdateWrapper<QuAnswer> u = new UpdateWrapper<>();
        u.lambda().eq(QuAnswer::getQuId,quVo.getQuId());
        quAnswerService.remove(u);
        List<QuAnswer> quAnswers = quVo.getQuAnswers();
        if(CollectionUtil.isNotEmpty(quAnswers)){
            quAnswers.forEach(s->{
                s.setQuId(quVo.getQuId());
                    quAnswerService.save(s);
            });
        }
    }
}
